package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Admin;
import com.abaaba.project2.bean.AdminExample;
import com.abaaba.project2.bean.Log;
import com.abaaba.project2.bean.LogExample;
import com.abaaba.project2.bean.bo.AdminBo;
import com.abaaba.project2.bean.bo.LogListBo;
import com.abaaba.project2.bean.bo.WxLoginBo;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.bean.vo.GeneralListVo;
import com.abaaba.project2.bean.vo.PermissionsVo;
import com.abaaba.project2.mapper.AdminMapper;
import com.abaaba.project2.mapper.LogMapper;
import com.abaaba.project2.service.AdminLogService;
import com.abaaba.project2.util.UserIdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminLogServiceImpl implements AdminLogService {

    String adminName;

    @Autowired
    LogMapper logMapper;

    @Autowired
    AdminMapper adminMapper;

    @Override
    public GeneralListVo<Log> getLogList(LogListBo logListBo) {

        //分页设置
        PageHelper.startPage(logListBo.getPage(),logListBo.getLimit());

        //封装example
        LogExample logExample = new LogExample();
        logExample.setOrderByClause(logListBo.getSort() + " " + logListBo.getOrder());
        LogExample.Criteria criteria = logExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        if(logListBo.getName() != null && !"".equals(logListBo.getName()))
        {
            criteria.andAdminLike("%"+logListBo.getName()+"%");
        }

        //调用Dao层
        List<Log> logs = logMapper.selectByExample(logExample);

        return new GeneralListVo<Log>(logs,(int)new PageInfo<Log>(logs).getTotal());

    }

    @Override
    public void systemLogCreate(BaseRespVo respVo,JoinPoint joinPoint) {

        Log log = new Log();
        if(!"adminLogout".equals(joinPoint.getSignature().getName()))
        {
            adminName = (String) SecurityUtils.getSubject().getPrincipal();
        }
        if("adminLogin".equals(joinPoint.getSignature().getName())) {
            Object[] args = joinPoint.getArgs();
            AdminBo adminBo = (AdminBo) args[0];
            adminName = adminBo.getUsername();
        }
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(adminName);
        List<Admin> admins = adminMapper.selectByExample(adminExample);

        log.setAdmin(admins.get(0).getUsername());
        log.setIp(SecurityUtils.getSubject().getSession().getHost());
        log.setType(1);
        log.setAction(joinPoint.getSignature().getName());
        log.setAddTime(new Date(System.currentTimeMillis()));

        if(respVo.getErrno() == 0){
            log.setStatus(true);
        }
        else
        {
            log.setStatus(false);
        }

        log.setResult(respVo.getErrmsg());

        logMapper.insertSelective(log);


    }
}
